package game.tileMap
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mk.gamemap.view.anime.IAnime;
	

	public class SpriteCache
	{
		[Embed(source="asset/TileA2.png")]
		public static const tileImage:Class;
		
		[Embed(source="asset/People5.png")]
		public static const actorImage:Class;
		
		
		
		
		private var source:BitmapData
		
		private var actorSource:BitmapData
		
		private var itemSource:BitmapData
		public static var TileSize:int=MapConst.TileSize;
		public function SpriteCache()
		{
			init()
		}
		
		
		public function init():void{
			var bitmap:Bitmap=new tileImage() as Bitmap;
			source=bitmap.bitmapData
				
			bitmap=new actorImage() as Bitmap;
			actorSource=bitmap.bitmapData
			
		}
		
		
		public function getTileBmd(id:int):BitmapData{
			var index:int=int(id)
			var bitMapData:BitmapData=new BitmapData(TileSize,TileSize);
			var rect:Rectangle = new Rectangle(16, (32+16)*index, TileSize,TileSize);
			
			if (index==1){
				rect.x=32*7-16;
				rect.y=32+16;
			}
			
			if (index==3){
				rect.x=32*12;
				rect.y=32*9;
			}
			
			if (index>3){
				rect.x=32*14;
				rect.y=96*(index-4);
			}
			
			var pt:Point = new Point(0, 0);
			bitMapData.copyPixels(source,rect,pt);
			return bitMapData
		}
		
		
		public function drawTile(id:int):Sprite{
			var bitMapData:BitmapData=getTileBmd(id);
			var image:Bitmap=new Bitmap(bitMapData);
			var view:Sprite=new Sprite
			view.addChild(image);
			return view;
		}
		
		public function produce():Sprite{
			
			var bitMapData:BitmapData=new BitmapData(TileSize,TileSize);
			var rect:Rectangle = new Rectangle(16, 32+16, TileSize,TileSize);
			var pt:Point = new Point(0, 0);
			bitMapData.copyPixels(source,rect,pt);
			var image:Bitmap=new Bitmap(bitMapData);
			var view:Sprite=new Sprite
			view.addChild(image);
			return view;
		}
		
		public function produceCharacter(id:String):Sprite{
			
			var bitMapData:BitmapData=new BitmapData(TileSize,TileSize);
			var rect:Rectangle = new Rectangle(0, 0, TileSize,TileSize);
			var pt:Point = new Point(0, 0);
			bitMapData.copyPixels(actorSource,rect,pt);
			var image:Bitmap=new Bitmap(bitMapData);
			var view:Sprite=new Sprite
			view.addChild(image);
			return view;
		}
		
		public function producePlayer():Sprite{
			
			var bitMapData:BitmapData=new BitmapData(TileSize,TileSize);
			var rect:Rectangle = new Rectangle(0, 0, TileSize,TileSize);
			var pt:Point = new Point(0, 0);
			bitMapData.copyPixels(actorSource,rect,pt);
			var image:Bitmap=new Bitmap(bitMapData);
			var view:Sprite=new Sprite
			view.addChild(image);
			return view;
		}
		
		public function produceDoor():Sprite{
			
			var bitMapData:BitmapData=new BitmapData(TileSize,TileSize);
			var rect:Rectangle = new Rectangle(14*32, 9*32, TileSize,TileSize);
			var pt:Point = new Point(0, 0);
			bitMapData.copyPixels(source,rect,pt);
			var image:Bitmap=new Bitmap(bitMapData);
			var view:Sprite=new Sprite
			view.addChild(image);
			return view;
		}
		
		public function produceKey():Sprite{
			
			var bitMapData:BitmapData=new BitmapData(TileSize,TileSize);
			var rect:Rectangle = new Rectangle(3*32, 3*32, TileSize,TileSize);
			var pt:Point = new Point(0, 0);
			bitMapData.copyPixels(itemSource,rect,pt);
			var image:Bitmap=new Bitmap(bitMapData);
			var view:Sprite=new Sprite
			view.addChild(image);
			return view;
		}
		
		public function produceChar(){
			
			var bitMapData:BitmapData=new BitmapData(TileSize,TileSize);
			var rect:Rectangle = new Rectangle(0*32, 0*32, TileSize*3,TileSize*3);
			var pt:Point = new Point(0, 0);
			bitMapData.copyPixels(actorSource,rect,pt);
			
		}
		
		public  function creatSpView(id:String):Sprite{
			var sp:Sprite;
			
			switch (id){
				case "0":
					break;
				case "1":
					sp=producePlayer();
					break;
				case "2":
					sp=produceDoor();
					break;
				case "3":
					sp=produceKey();
					break;
			}
			return sp;
		}
	}
}